﻿(function() {
var map = Default.map;
var $ = Go.$;
var My = {
  venues : {}
}

function VenueTab(isActive){
  if(isActive) this.active();
}
window.VenueTab = VenueTab;
VenueTab.prototype = new GoTab(Default.key.venue, {type:G_HYBRID_MAP}, true);
VenueTab.prototype.load = function(){
  My.venues = {};
  this.initHead();
  this.initBoard();
  var mgr = VenueMgr.getInstance();
  for (var i in DVenue.item) {
    DVenue.item[i].marker.show();
    if (!My.venues[i]) {
      My.venues[i] = i;
    }
  }
  this.resizeMap(My.venues);
  var callFuc = new Go.refreshPlacemark(DVenue.item, true)
  setTimeout(callFuc, 500);
}
VenueTab.prototype.reload = function(){
  for (var i in My.venues) {
  	var loca = DVenue.item[i];
  	loca.marker.show();
  }
  var callFuc = new Go.refreshPlacemark(My.venues, true)
  setTimeout(callFuc, 500);
}
VenueTab.prototype.clear = function(){
  for (var i in My.venues) {
  	var loca = DVenue.item[i];
  	loca.marker.hide();
  }
  var callFuc = new Go.refreshPlacemark(My.venues, false)
  setTimeout(callFuc, 500);
}
VenueTab.prototype.initHead = function() {
  var head = $("panelHead");
  var prefix = DVenue.prefix;
//  Go.$n("p", head, null, Go.getLocalLabel("guide", prefix));
  var row = Go.$n("p", head, "head-select");
  var select = Go.$n("span", row, "", Go.getLocalLabel("selectDist", prefix) + ": ");
  var dropTip = Go.$n("span",select, null, Go.getLocalLabel("all", prefix), {id:"venueRegion"});
  var drop = Go.$n("span", select, null, "▼", {style:"cursor:pointer"});
  this.initPanel(row);
  
  GEvent.addDomListener(dropTip, "mouseover", function() {
    dropTip.style.cursor = "pointer";
    dropTip.style.borderBottom = "solid #000 1px";
  });
  GEvent.addDomListener(dropTip, "mouseout", function() {
    dropTip.style.borderBottom = "0";
  });
  GEvent.addDomListener(dropTip, 'click', GEvent.callback(this, this.dropClickCallback));
  GEvent.addDomListener(drop, 'click', GEvent.callback(this, this.dropClickCallback));
}
VenueTab.prototype.initPanel = function(prt) {
  var panel = Go.$n("div", prt, "ctl-panel", null, {id:"venueRegionDiv"});
  panel.style.cssText = "display:none";
  var category = DVenue.category;
  var prefix = DVenue.prefix;
  for (var i in category) {
    var cate = category[i];
    cate.label = Go.getLocalLabel(cate.sn, prefix);
    new GoLine(cate.label, panel, GEvent.callback(this, this.lineClickCallback), i);
  }
  Go.$0(panel);
}
VenueTab.prototype.initBoard = function() {
  var board = $("listContainer");
  var mgr = VenueMgr.getInstance();
  for (var i in DVenue.item) {
    var loca = DVenue.item[i];
    if (!My.venues[i]) {
      My.venues[i] = i;
    }
    if (!loca.label) {
      var prefix = DVenue.prefix;
      loca.label = Go.getLocalLabel(i, prefix);
    }
    loca.marker.show();
    this.createVenueNode(i, loca, board, false, mgr)
  }
  var callFuc = new Go.refreshPlacemark(DVenue.item, true)
  setTimeout(callFuc, 500);
}
VenueTab.prototype.createVenueNode = function(sn, loca, parent, isFromStar, mgr){
  var nodeId = isFromStar ? sn + "_" + this.key + "_t" : sn + "_" + this.key;
  var node = Go.$n("p", parent, null, null, {id:nodeId});
  node.style.cssText = 'padding:3px 5px;border-bottom:solid #ccc 1px;line-height:20px;';
//  var star = this.genStarIcon(sn, this.key, isFromStar, node);
  var name = Go.$n("div", node, "venue-name", loca.label);
  var subNode = Go.$n("div", node, "sport-item");
  var num = 0;
  for (var j in loca.sp) {
    //icon = Default.baseUrl+"icons/"+DSport.sports[j].icon;
    //Go.$n("img", subNode, null, null, {src:icon, title:Go.getLocalLabel("s", j)});
    var icon = IconFactories['sport'].getIcon(j.toUpperCase());
    icon.style.cssFloat = "left";
    icon.style.styleFloat = "left";
    subNode.appendChild(icon);
    num++;
  }
  name.style.width = (279 - num*20/* - star.offsetWidth*/-50) + "px";
  Go.$n("div", node, "clear-div");
  
  GEvent.addDomListener(node, "mouseover", GEvent.callbackArgs(mgr, mgr.highlightLoca, nodeId));
  GEvent.addDomListener(node, "mouseout", GEvent.callbackArgs(mgr, mgr.unHighlightLoca, nodeId));
  GEvent.addDomListener(node, "click", GEvent.callbackArgs(mgr,mgr.activeLoca, nodeId));
  return node;
}

VenueTab.prototype.dropClickCallback = function() {
  var panel = $("venueRegionDiv");
  if (panel.style.display == "none") {
    this.showPanel(panel);
  } else {
    this.hidePanel(panel);
  }
}
VenueTab.prototype.lineClickCallback = function(id) {
  for(var i in My.venues){
    this.hideLoca(i);
  }
  $("venueRegion").innerHTML = DVenue.category[id].label;
  this.hidePanel($("venueRegionDiv"));
  if(map.getCurrentMapType() != G_SATELLITE_3D_MAP)
    map.closeInfoWindow();
  var list = DVenue.category[id].sub;
  var data = DVenue.item;
  if(list){
    for (var i = 0, sn; sn = list[i]; i++) {
      this.showLoca(sn);
    }
  } else {
    for(var i in data){
      this.showLoca(i);
    }
  }
  this.resizeMap(My.venues);
}
VenueTab.prototype.hidePanel = function(panel) {
  panel.style.display = "none";
}
VenueTab.prototype.showPanel = function(panel) {
  var dropTip = $("venueRegion");
  panel.style.display = "block";
  panel.style.left = dropTip.offsetLeft + "px";
  if(document.all){
      panel.style.top = "15px";
  }
  //panel.style.top = (dropTip.offsetTop + dropTip.offsetHeight) + "px";
}
VenueTab.prototype.showLoca = function(sn) {
  var loca = DVenue.item[sn];
  var nodeId = sn +"_"+this.key;
  loca.marker.show();
  if(loca.placemark){
      loca.placemark.setVisibility(true);
  }
  $(nodeId).style.display = "";
  My.venues[sn] = sn;
}
VenueTab.prototype.hideLoca = function(sn) {
  var loca = DVenue.item[sn];
  var nodeId = sn +"_"+this.key;
  loca.marker.hide();
  if(loca.placemark){
      loca.placemark.setVisibility(false);
  }
  $(nodeId).style.display = "none";
  if (My.venues[sn]) {
    delete My.venues[sn];
  }
}
VenueTab.prototype.openInfowindow = function(sn) {
  var loca = DVenue.item[sn];
  var marker = loca.marker;
  var videoId = VenueMgr.getInstance().getVideoId(sn);

  var html = Go.$n("div");
  html.style.cssText = "font-size:12px;";
  var tit = Go.$n("div", html);
  tit.style.cssText = "font-weight:bold;margin:8px 0";
  tit.innerHTML = Go.getLocalLabel(sn, "v");
  var tab = Go.$n("div", html);
  tab.style.cssText = "background-color:#e8ecf9;padding-top:8px";
  var startindex = 1;
  if(videoId){
    tab.innerHTML = '<div class="info-running" style="margin-left:9px;" id="tab1" onclick="Go.changeTab(1, 3)">'+Go.getLocalLabel("videos")+'</div>' +
	        '<div class="info-tab" style="margin-left:5px;" id="tab2" onclick="Go.changeTab(2, 3)">'+Go.getLocalLabel("photo")+'</div>' +
    		'<div class="info-tab" style="margin-left:5px;" id="tab3" onclick="Go.changeTab(3, 3)">'+Go.getLocalLabel("info")+'</div>'
    var cont1 = Go.$n("div", html, "info-content", null/*this.getVideoHtml(sn)*/, {"id": "content1"});
    showFlash(cont1,sn,290, 190);
    cont1.style.cssText = "height:190px;overflow:auto;";
    startindex = 2;
  }
  else{
     tab.innerHTML = '<div class="info-running" style="margin-left:5px;" id="tab1" onclick="Go.changeTab(1, 2)">'+Go.getLocalLabel("photo")+'</div>' +
    		'<div class="info-tab" style="margin-left:5px;" id="tab2" onclick="Go.changeTab(2, 2)">'+Go.getLocalLabel("info")+'</div>';
  }
  var cont2 = Go.$n("div", html, "info-content", "", {"id": "content" + startindex});
  if(videoId){
      cont2.style.cssText = "display:none;height:190px;overflow:auto;text-align:center";
  }
  else{
      cont2.style.cssText = "height:190px;overflow:auto;text-align:center";
  }
  var link = Go.$n("a", cont2, "", "", {"href": "http://en.beijing2008.cn/", "target": "_blank"});
  var img = _IG_GetImage(Default.imageUrl+'stadiums/' + sn + '.jpg');
  img.style.cssText = "width:260px;border:1px solid #aaaaaa;padding:2px;display:inline;float:none";
  link.appendChild(img);
  var copyright = Go.$n("p", cont2, "", '&copy;&nbsp;<a href="http://en.beijing2008.cn/" target="_blank">Beijing2008</a>');
  copyright.style.cssText = "float:left;color:#666;font-size:0.8em;margin-right:10px;";
  var cont3 = Go.$n("div", html, "info-content", this.getLocaInfomation(sn), {"id": "content" + (startindex + 1)});
  cont3.style.cssText = "display:none;height:190px;overflow:auto;";     
  map.openInfoWindow(marker.getLatLng(), html, {"maxWidth": 300});
}

VenueTab.prototype.getVideoHtml = function(parent, sn, width, height, mode){
//  var txt = VenueMgr.getInstance().getVideoId(sn);
  var id = VenueMgr.getInstance().getVideoId(sn);
 var url = "http://www.youtube.com/v/" + id;
 var arr = new Array();  
 var str="";  
 arr.push("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"" + width + "\" height=\"" + height + "\"> ");  
 arr.push("<param name=\"movie\" value=\"" + url + "\">");  
 arr.push("<param name=\"quality\" value=\"high\"> ");  
 if (mode) {  
 arr.push("<param name=\"wmode\" value=\"transparent\">");  
 }  
 arr.push("<param name=\"menu\" value=\"false\">");  
 arr.push("<embed src=\"" + url + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\"></embed>");  
 arr.push("</object>");  
 str = arr.join("");  
 parent.innerHTML = str;
 
//  return '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/' + txt + '"></param><embed src="http://www.youtube.com/v/' + txt + '" type="application/x-shockwave-flash" width="425" height="350"></embed></object>';
}

var showFlash = function (parent, sn, width, height, mode) {  
 var id = VenueMgr.getInstance().getVideoId(sn);
 var url = "http://www.youtube.com/v/" + id;
 var arr = new Array();  
 var str="";  
 arr.push("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"" + width + "\" height=\"" + height + "\"> ");  
 arr.push("<param name=\"movie\" value=\"" + url + "\">");  
 arr.push("<param name=\"quality\" value=\"high\"> ");  
 if (mode) {  
 arr.push("<param name=\"wmode\" value=\"transparent\">");  
 }  
 arr.push("<param name=\"menu\" value=\"false\">");  
 arr.push("<embed src=\"" + url + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\"></embed>");  
 arr.push("</object>");  
 str = arr.join("");  
 parent.innerHTML = str;  
}; 
VenueTab.prototype.getLocaInfomation = function(sn){
  return '<div style="margin-top:12px;">' + Go.getLocalLabel("v-"+sn+"-desc")+ '</div>';
}

var GoLine = function(label, cnt, clickCallback, sn) {
  var line = Go.$n("p", cnt, "line-item", label)
  GEvent.addDomListener(line, "mouseover", function() {
    line.className = "highlight";
  });
  GEvent.addDomListener(line, "mouseout", function() {
    line.className = "un-highlight";
  });
  GEvent.addDomListener(line, "click", function() {
    clickCallback(sn);
  });
}
})();
Default.tabs[Default.key.venue] = new VenueTab();
